/*
 * Batch.java
 *
 * Created on March 29, 2007, 12:14 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.samlam;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Brian Barrett
 */
public class Batch {
    
    /** Creates a new instance of SalesBatchTester */
    public Batch() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws DataException, SQLException, Exception {
        // TODO code application logic here
        StoreProduct storeprod = StoreProductDAO.getInstance().create();
        List<StoreProduct> list = new LinkedList();
        
        list = StoreProductDAO.getInstance().getAlltoOrder();
        
        //loop through vendor items where conceptualid = the storeproduct's conceptualid, find the
        //one with the lowest cost
        
        //this for loop will go through the rs and create a string of all the conceptualID's that need to
        //ordered. if we're on the last of the list, we don't include the extra spacing or the words or
        String toOrderId = "";
        for(int i = 0; i<list.size(); i++) {
            if(i == list.size()){
                toOrderId += list.get(i).getConceptualprod().getCpguid();
            }else {
                toOrderId += (list.get(i).getConceptualprod().getCpguid() + ", or ");
            }//end else
        }//end for
        
        Connection conn = ConnectionPool.getInstance().get();
        PreparedStatement query = conn.prepareStatement("Select min(cost), vendorID, conceptualID from VendorItem where conceptualID = ? order by VendorID");
        query.setString(1, toOrderId);
        ResultSet rs = query.executeQuery();
        
        
        String globalVendor = null;
        
        while(rs.next()) {
            PurchaseOrderBO po = null;
            List<OrderLineBO> oList = null;
            if(globalVendor == null) {
                globalVendor = rs.getString("vendor");
                po = PurchaseOrderDAO.getInstance().create();
                oList = new LinkedList();
                VendorBO vend = VendorDAO.getInstance().read(rs.getString("vendor"), conn);
                po.setVendor(vend);
                
            }
            if(globalVendor == rs.getString("vendor")) {
                VendorItemBO v = VendorItemDAO.getInstance().create();
                v.setAlreadyInDB(rs.getBoolean("AlreadyinDB"));
                v.setConceptualprod(ConceptualProductDAO.getInstance().read(rs.getString("ConceptualProdID"), conn));
                v.setCost(rs.getDouble("Cost"));
                v.setDirty(rs.getBoolean("dirty"));
                v.setVendor(VendorDAO.getInstance().read(rs.getString("vendorID"), conn));        
                
                OrderLineBO ol = OrderLineDAO.getInstance().create();
                ol.setAlreadyInDB(false);
                ol.setDirty(true);
                ol.setOrder(po);
                ol.setQuantity(StoreProductDAO.getInstance().read(rs.getString("ConceptualProductID")).getQtytoorder());
                ol.setVendoritem(v);
                
                oList.add(ol);
            }else{
                
                po.setAlreadyInDB(false);
                po.setDirty(true);
                po.setOrderlines(oList);
                
                PurchaseOrderDAO.getInstance().save(po, conn); 
                
                po = PurchaseOrderDAO.getInstance().create();
                oList = new LinkedList();
            }
        }
        
        
    }
    
}
